﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OF.Security.Utils
{
    public static class DateTimeHelper
    {
        /// <summary>
        /// 将日期时间类型转换为可供数据库授受的类型
        /// </summary>
        /// <param name="Date"></param>
        /// <returns></returns>
        public static object ToNullDbDateTime(this DateTime Date)
        {
            object oDate = Date;
            //if (Date == DateTime.MinValue)
            //{
            //    oDate = DBNull.Value;
            //}
            if (Date.IsNullDateTime())
            {
                oDate = DBNull.Value;
            }
            return oDate;
        }
        /// <summary>
        /// 判断日期时间类型是否为空
        /// </summary>
        /// <param name="Date"></param>
        /// <returns></returns>
        public static bool IsNullDateTime(this DateTime Date)
        {
            bool bReturn = Date == Convert.ToDateTime("1900-1-1") || Date == DateTime.MinValue;
            return bReturn;
        }
        /// <summary>
        /// 按指定格式显示时间
        /// </summary>
        /// <param name="Date"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static string ToDisplay(this DateTime Date, string format)
        {
            string szReturn = string.Empty;
            if (Convert.ToDateTime("1900-1-1") == Date || Date == DateTime.MinValue)
            {

            }
            else
            {
                szReturn = Date.ToString(format);
            }
            return szReturn;

        }
        /// <summary>
        /// 将字符串转化为时间
        /// </summary>
        /// <param name="Date"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string Date)
        {
            DateTime dtReturn=new DateTime();
            DateTime.TryParse(Date, out dtReturn);
            return dtReturn;

        }
    }
}
